package org.zebra.mybatis.plus.druid;

import com.alibaba.druid.filter.logging.LogFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * @author zhanghongbin
 */
public class Slf4jLogInfoLevelFilter extends LogFilter {

    private Logger dataSourceLogger;
    private Logger connectionLogger;
    private Logger statementLogger;
    private Logger resultSetLogger;

    public Slf4jLogInfoLevelFilter(final Properties properties) {
        super(properties);
        this.dataSourceLogger = LoggerFactory.getLogger(this.dataSourceLoggerName);
        this.connectionLogger = LoggerFactory.getLogger(this.connectionLoggerName);
        this.statementLogger = LoggerFactory.getLogger(this.statementLoggerName);
        this.resultSetLogger = LoggerFactory.getLogger(this.resultSetLoggerName);
        this.setLogOutputStyle();
    }

    public Slf4jLogInfoLevelFilter() {
        this.dataSourceLogger = LoggerFactory.getLogger(this.dataSourceLoggerName);
        this.connectionLogger = LoggerFactory.getLogger(this.connectionLoggerName);
        this.statementLogger = LoggerFactory.getLogger(this.statementLoggerName);
        this.resultSetLogger = LoggerFactory.getLogger(this.resultSetLoggerName);
        this.setLogOutputStyle();
    }

    /**
     * 写死，采用sql完整输出
     */
    private void setLogOutputStyle() {
        this.setStatementExecutableSqlLogEnable(true);
        this.setResultSetLogEnabled(false);
        this.setConnectionLogEnabled(false);
        this.setDataSourceLogEnabled(false);
        this.setStatementCreateAfterLogEnabled(false);
        this.setStatementPrepareAfterLogEnabled(false);
        this.setStatementPrepareCallAfterLogEnabled(false);
        this.setStatementExecuteAfterLogEnabled(false);
        this.setStatementExecuteQueryAfterLogEnabled(false);
        this.setStatementExecuteUpdateAfterLogEnabled(false);
        this.setStatementExecuteBatchAfterLogEnabled(false);
        this.setStatementCloseAfterLogEnabled(false);
        this.setStatementParameterSetLogEnabled(false);
        this.setStatementParameterClearLogEnable(false);
        this.setStatementLogErrorEnabled(true);
        this.setStatementLogEnabled(true);
    }

    public String getDataSourceLoggerName() {
        return this.dataSourceLoggerName;
    }

    public void setDataSourceLoggerName(String dataSourceLoggerName) {
        this.dataSourceLoggerName = dataSourceLoggerName;
        this.dataSourceLogger = LoggerFactory.getLogger(dataSourceLoggerName);
    }

    public void setDataSourceLogger(Logger dataSourceLogger) {
        this.dataSourceLogger = dataSourceLogger;
        this.dataSourceLoggerName = dataSourceLogger.getName();
    }

    public String getConnectionLoggerName() {
        return this.connectionLoggerName;
    }

    public void setConnectionLoggerName(String connectionLoggerName) {
        this.connectionLoggerName = connectionLoggerName;
        this.connectionLogger = LoggerFactory.getLogger(connectionLoggerName);
    }

    public void setConnectionLogger(Logger connectionLogger) {
        this.connectionLogger = connectionLogger;
        this.connectionLoggerName = connectionLogger.getName();
    }

    public String getStatementLoggerName() {
        return this.statementLoggerName;
    }

    public void setStatementLoggerName(String statementLoggerName) {
        this.statementLoggerName = statementLoggerName;
        this.statementLogger = LoggerFactory.getLogger(statementLoggerName);
    }

    public void setStatementLogger(Logger statementLogger) {
        this.statementLogger = statementLogger;
        this.statementLoggerName = statementLogger.getName();
    }

    public String getResultSetLoggerName() {
        return this.resultSetLoggerName;
    }

    public void setResultSetLoggerName(String resultSetLoggerName) {
        this.resultSetLoggerName = resultSetLoggerName;
        this.resultSetLogger = LoggerFactory.getLogger(resultSetLoggerName);
    }

    public void setResultSetLogger(Logger resultSetLogger) {
        this.resultSetLogger = resultSetLogger;
        this.resultSetLoggerName = resultSetLogger.getName();
    }

    public boolean isConnectionLogErrorEnabled() {
        return this.connectionLogger.isErrorEnabled() && super.isConnectionLogErrorEnabled();
    }

    public boolean isDataSourceLogEnabled() {
        return this.dataSourceLogger.isDebugEnabled() && super.isDataSourceLogEnabled();
    }

    public boolean isConnectionLogEnabled() {
        return this.connectionLogger.isDebugEnabled() && super.isConnectionLogEnabled();
    }

    public boolean isStatementLogEnabled() {
        return super.isStatementLogEnabled();
    }

    public boolean isResultSetLogEnabled() {
        return this.resultSetLogger.isDebugEnabled() && super.isResultSetLogEnabled();
    }

    public boolean isResultSetLogErrorEnabled() {
        return this.resultSetLogger.isErrorEnabled() && super.isResultSetLogErrorEnabled();
    }

    public boolean isStatementLogErrorEnabled() {
        return this.statementLogger.isErrorEnabled() && super.isStatementLogErrorEnabled();
    }

    protected void connectionLog(String message) {
        this.connectionLogger.debug(message);
    }

    protected void statementLog(String message) {
        this.statementLogger.info(message);
    }

    protected void resultSetLog(String message) {
        this.resultSetLogger.debug(message);
    }

    protected void resultSetLogError(String message, Throwable error) {
        this.resultSetLogger.error(message, error);
    }

    protected void statementLogError(String message, Throwable error) {
        this.statementLogger.error(message, error);
    }
}
